﻿using System;
using EmbeddedSharp.Core;

namespace EmbeddedSharp.Scripting.Function.Context {

    /// <summary>
    /// Evaluate an expression
    /// </summary>
    [Function("evaluate", "Evaluate an expression", "evaluate(\"1+4\")", "5")]
    public class EvaluateNode : ContextFuncNode {

        /// <summary>
        /// Constructor
        /// </summary>
        public EvaluateNode()
            : base("evaluate") {
        }

        /// <summary>
        /// Evaluate an expression
        /// </summary>
        /// <returns></returns>
        public override object Evaluate() {
            if (Parameters.Count < 0) {
                throw new ArgumentException(
                    "Usage: evaluate(stmt)");
            }

            string stmt = Convert.ToString(
                Parameters[0].Evaluate());

            return Context.Evaluate(stmt);
        }
    }
}
